package net.onceface.application.assembler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.onceface.application.dto.CartDTO;
import net.onceface.core.domain.Cart;
import net.onceface.core.domain.Product;
import net.onceface.core.domain.Spec;

public class CartAssembler {
	
	/**
	 * 商品加入购物车
	 * 传入商品和用户ID 返回加入购物车的对象
	 * **/
	public static Cart toAddCartObject(Long clientId, Product product, Spec spec, int quantity){
		Cart cart = new Cart(clientId, product.getId(), product.getName(),quantity,product.getPicture());
		cart.setValid(true);
		if(spec != null){
			if((spec.getStock() - quantity) < 1){
				return null;
			}
			cart.setSpecID(spec.getId());
			cart.setSpecName(spec.getName());
			if(spec.getPrice() > 0){
				cart.setProductPrice(spec.getPrice());
			}else{
				spec.setOriginalPrice(spec.getOriginalPrice());
			}
		}else{
			if((product.getStock() - quantity) < 1){
				return null;
			}
			if(product.getPrice() > 0){
				cart.setProductPrice(product.getPrice());
			}else{
				cart.setProductPrice(product.getOriginalPrice());
			}
		}
		return cart;
	}

	public static CartDTO toDTO(Cart cart){
		if(cart == null){
			return null;
		}
		CartDTO result = new CartDTO();
		result.setId(cart.getId());
		result.setChecked(cart.isChecked());
		result.setVersion(cart.getVersion());
		result.setClientId(cart.getClientId());
		result.setValid(cart.isValid());
		result.setCreateDate(cart.getCreateDate());
		result.setProductId(cart.getProductId());
		result.setProductName(cart.getProductName());
		result.setProductPicture(cart.getProductPicture());
		result.setProductPrice(cart.getProductPrice());
		result.setQuantity(cart.getQuantity());
		result.setSpecName(cart.getSpecName());
		result.setSpecID(cart.getSpecID());
		return result;
	}
	
	public static Cart toEntity(CartDTO cart){
		if(cart == null){
			return null;
		}
		Cart result = new Cart();
		result.setId(cart.getId());
		result.setVersion(cart.getVersion());
		result.setChecked(cart.isChecked());
		result.setClientId(cart.getClientId());
		result.setValid(cart.isValid());
		result.setCreateDate(cart.getCreateDate());
		result.setProductId(cart.getProductId());
		result.setProductName(cart.getProductName());
		result.setProductPicture(cart.getProductPicture());
		result.setProductPrice(cart.getProductPrice());
		result.setQuantity(cart.getQuantity());
		result.setSpecName(cart.getSpecName());
		result.setSpecID(cart.getSpecID());
		return result;
	}
	
	public static List<Cart>  toEntities(Collection<CartDTO>  carts){
		if (carts == null) {
			return null;
		}
		List<Cart> results = new ArrayList<Cart>();
		for (CartDTO each : carts) {
			results.add(toEntity(each));
		}
		return results;
	}
	
	public static List<CartDTO>  toDTOs(Collection<Cart>  carts){
		if (carts == null) {
			return null;
		}
		List<CartDTO> results = new ArrayList<CartDTO>();
		for (Cart each : carts) {
			results.add(toDTO(each));
		}
		return results;
	}
}
